<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      房屋管理
      <div class="department-top-btn">
        <el-button class="btn-default" plain icon="el-icon-plus" size="mini" @click="addHouse()">新增</el-button>
        <el-button class="btn-default" size="mini" plain icon="el-icon-download">
          <a class="btn-down" href="resource/houseTemp.xlsx" download="房屋导入模板">下载模板</a>
        </el-button>
        <el-button class="btn-default" size="mini" plain icon="el-icon-upload2" @click="bulkImport">批量导入</el-button>
      </div>
    </div>
    <!-- 搜索框 -->
    <el-form :model="houseForm" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="5">
          <el-form-item label="房屋编号">
            <el-input v-model="houseForm.houseNo" placeholder="房屋编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="楼栋编号">
            <el-input v-model="houseForm.mobile" placeholder="楼栋编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="所属小区">
            <AreaSelect :selectModel="houseForm.deptId" :areaIds="areaIds" @onAreaChange="onAreaChange" hideThird="true"> </AreaSelect>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="houseSearch">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- 列表 -->
    <div style="margin: 10px 30px">
      <el-table :data="userListTable" border style="width: 100%">
        <el-table-column prop="houseNo" label="房屋编号" min-width="100" align="center"></el-table-column>
        <el-table-column prop="buildingNo" label="所在楼栋" min-width="100" align="center"></el-table-column>
        <el-table-column prop="floor" label="所在楼层" min-width="100" align="center"></el-table-column>
        <el-table-column prop="deptName" label="所属小区" min-width="100" align="center"></el-table-column>
        <el-table-column prop="peopleNumber" label="最大居住人数" min-width="140" align="center"></el-table-column>
        <el-table-column prop="houseArea" label="房屋面积(㎡)" min-width="140" align="center"></el-table-column>
        <el-table-column prop="houseLabel" label="房屋类别" min-width="140" align="center" :formatter="buildTypeToText"></el-table-column>
        <el-table-column prop="housePurpose" label="房屋用途" min-width="140" align="center" :formatter="usageTypeToText"></el-table-column>
        <el-table-column prop="note" label="备注" min-width="140" align="center"></el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="houseEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="houseDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page.sync="pagination.currentPage"
        :page-size="10"
        layout="total, prev, pager, next,jumper"
        :total="pagination.total"
        class="el-page-body"
      ></el-pagination>
    </div>
    <!-- 批量导入房屋 -->
    <el-dialog title="批量导入" :visible.sync="houseVisible" :close-on-click-modal="false" :append-to-body="false">
      <el-row :gutter="10">
        <el-col :span="16"><el-input v-model="templatePath" placeholder="" /></el-col>
        <el-col :span="8">
          <el-button type="primary" class="box">
            选择模板
            <input class="btn-upload" type="file" ref="fileValue" @change="handleUpload" />
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'

export default {
  name: 'houseList',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      houseVisible: false,
      templatePath: '',
      userListTable: [],
      houseForm: {
        buildId: '',
        deptId: '',
        houseNo: ''
      },
      areaIds: {
        areaId1: '',
        areaId2: '',
        areaId3: ''
      },
      pagination: {
        currentPage: 1,
        total: null
      }
    }
  },
  created () {},
  methods: {
    addHouse () {
      this.$router.push({
        name: 'addModifyHouse',
        params: {
          id: 'addHouse'
        }
      })
    },
    // 批量导入房屋
    bulkImport () {
      this.houseVisible = true
    },
    // 选择模板
    handleUpload (e) {
      const ACCEPT = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']
      const [file] = e.target.files
      if (!file) {
        return
      }
      const { type: fileType, name: fileName } = file
      this.templatePath = fileName
      if (!ACCEPT.includes(fileType)) {
        this.$message({
          message: `不支持${fileType}文件类型，请重新选择`,
          type: 'error',
          duration: 2000
        })
        return
      }
      this.upLoadTemp(file)
    },
    // 上传模板
    upLoadTemp (file) {
      const url = this.$base.getUrl('bizhouseinfoUploadHouses')
      let params = new FormData()
      params.append('file', file)
      this.$store
        .dispatch('postUploadForm', {
          url,
          data: params
        })
        .then((res) => {
          if (res.code === 0) {
            this.$message.success('模板导入成功！')
            this.templatePath = ''
            this.$refs.fileValue.value = ''
            this.houseVisible = false
            this.pagination.currentPage = 1
            this.getHouseList()
          }
        })
        .catch((err) => {
          this.$refs.fileValue.value = ''
          this.$message.error(err.msg)
        })
    },
    houseEdit (row) {
      this.$router.push({
        name: 'addModifyHouse',
        params: {
          id: row.id
        }
      })
    },
    houseDelete (row) {
      const id = row.id
      this.$confirm('删除该房屋?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const url = this.$base.getUrl('bizhouseinfoDelete')
          let params = {
            isJson: true,
            url: url,
            data: {
              id: id
            }
          }
          this.$store.dispatch('postForm', params).then((res) => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.getHouseList()
            }
          })
        })
        .catch(() => {
          this.$message({
            message: '已取消删除',
            type: 'info'
          })
        })
    },
    // 房屋类别
    buildTypeToText (row) {
      return this.$base.getNameByConst('select', 'buildType', row.houseLabel)
    },
    // 房屋用途
    usageTypeToText (row) {
      return this.$base.getNameByConst('select', 'usageType', row.housePurpose)
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getHouseList()
    },
    getHouseList () {
      const url = this.$base.getUrl('bizhouseinfoList')
      let params = {
        isJson: true,
        url: url,
        data: {
          houseNo: this.houseForm.houseNo,
          deptId: this.houseForm.deptId,
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    houseSearch () {
      this.pagination.currentPage = 1
      this.getHouseList()
    },
    onAreaChange (val) {
      this.houseForm.deptId = val
    }
  },
  mounted () {
    this.getHouseList()
  }
}
</script>
<style scoped>
.g-content-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.department-top-btn {
  float: right;
}

.department-top-btn .btn-default {
  display: inline-block;
  padding: 2px 12px;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.42857143;
  text-align: center;
  cursor: pointer;
  background: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  margin-right: 10px;
}
.btn-default .btn-down {
  color: #409eff;
  text-decoration: none;
}
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

.el-table >>> th {
  text-align: center;
}
.box {
  position: relative;
}
.btn-upload {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
</style>
